/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.deposit;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.deposit.Deproduct;
import src.domain.gl.Glaccnt;
import src.domain.loan.Loanprodrate;
import src.services.deposit.DeproductModel;
import src.services.gl.GlaccntConverter;
import src.services.gl.GlaccntModel;
import src.services.gl.TaxModel;
import src.util.Constanta;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class DeproductBean implements Serializable {

    private List<Deproduct> deproducts;
    private List<SelectItem> avblGL, avblTax, avblPeriod;
    private Deproduct selectedData, unsaveData;
    
    private Glaccnt depGlaccount, interestGlaccount, penaltyGlaccount; 
    private List<Glaccnt> avblAkun; 
    

    private boolean editMode = true;
    
    private GlaccntModel glHelper;
    private TaxModel taxHelper;
    private DeproductModel helper;
    
    public DeproductBean() {
        this.helper = new DeproductModel();
        this.glHelper = new GlaccntModel();
        this.taxHelper = new TaxModel();
        this.deproducts = helper.findAll(null);
        this.avblPeriod = Constanta.getDepPeriode();
        this.avblGL = glHelper.listAll();
        this.avblTax = taxHelper.listAll();
        this.selectedData = null;
        this.unsaveData = null;
        this.avblAkun = GlaccntConverter.glaccountDB;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Deproduct> getDeproducts() {
        return this.deproducts;
    }
    
    public Deproduct getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Deproduct selectedData) {
        this.selectedData = selectedData;
    }

    public List<SelectItem> getAvblGL() {
        return avblGL;
    }

    public List<SelectItem> getAvblPeriod() {
        return avblPeriod;
    }

    public List<SelectItem> getAvblTax() {
        return avblTax;
    }

    public Deproduct getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Deproduct unsaveData) {
        this.unsaveData = unsaveData;
    }
    
    public TaxModel getTaxHelper() {
        return taxHelper;
    }
        
    public String getLisLabeltName(List<SelectItem> avblList, Integer id) {
        String nama = "";
        if (id!=null && !id.equals(0)) {
            int idx=0;
            boolean found=false;
            while (idx<avblList.size() && !found) {
                SelectItem it = avblList.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) nama = it.getLabel();
                idx++;
            }
        }
        return nama;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
        this.editMode = true; 
    }
    
    public void initForm() {
        this.avblGL = glHelper.listAll();
        this.avblTax = taxHelper.listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.unsaveData = Deproduct.clone(selectedData);
        
        if (unsaveData.getDepGlaccountId() != null)         this.depGlaccount = glHelper.getById(unsaveData.getDepGlaccountId()); 
        if (unsaveData.getInterestGlaccountId() != null)    this.interestGlaccount = glHelper.getById(unsaveData.getInterestGlaccountId()); 
        if (unsaveData.getPenaltyGlaccountId() != null)     this.penaltyGlaccount = glHelper.getById(unsaveData.getPenaltyGlaccountId()); 
    }
    
    public void newData() {  
        initForm();
        this.editMode = false;
        this.selectedData = null;
        this.unsaveData = new Deproduct();
        this.depGlaccount= new Glaccnt(); 
        this.interestGlaccount= new Glaccnt(); 
        this.penaltyGlaccount= new Glaccnt();  
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        unsaveData.setInterestGlaccountId(interestGlaccount.getGlaccntId()); 
        unsaveData.setPenaltyGlaccountId(penaltyGlaccount.getGlaccntId()); 
        unsaveData.setDepGlaccountId(depGlaccount.getGlaccntId()); 
        boolean success = helper.saveData(this.unsaveData);
        if (success==true) {
            if (this.editMode==false) this.deproducts.add(unsaveData);
            else {
                int i = deproducts.indexOf(selectedData);
                if (i <= deproducts.size()) deproducts.set(i, Deproduct.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setTaxId(null);
        } 
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        if (success) {
            this.deproducts.remove(this.selectedData);
            this.selectedData=null;
        }
               
        if(!success) Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }

    public List<Glaccnt> getAvblAkun() {
        return avblAkun;
    }

    public Glaccnt getDepGlaccount() {
        return depGlaccount;
    }

    public void setDepGlaccount(Glaccnt depGlaccount) {
        this.depGlaccount = depGlaccount;
    }

    public Glaccnt getInterestGlaccount() {
        return interestGlaccount;
    }

    public void setInterestGlaccount(Glaccnt interestGlaccount) {
        this.interestGlaccount = interestGlaccount;
    }

    public Glaccnt getPenaltyGlaccount() {
        return penaltyGlaccount;
    }

    public void setPenaltyGlaccount(Glaccnt penaltyGlaccount) {
        this.penaltyGlaccount = penaltyGlaccount;
    }
    
}
